/*
    libhdvbd - Library for building sophisticated DVB applications
    Copyright (C) 2012 Tony Houghton <h@realh.co.uk>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "chanscan-factory.h"
#include "chansdb.h"
#include "freesatscantodb.h"
#include "hdvb-freesat-scan.h"
#include "hdvb-freeview-scan.h"
#include "i18n.h"

static GHashTable *hdvb_chanscan_factory_registry = NULL;
static GHashTable *hdvb_chanscan_dbcreator_registry = NULL;
static GHashTable *hdvb_chanstodb_registry = NULL;

static void
hdvb_chanscan_factory_init_registry(void)
{
    if (!hdvb_chanscan_factory_registry)
    {
        hdvb_chanscan_factory_registry =
                g_hash_table_new(g_str_hash, g_str_equal);
    }
    if (!hdvb_chanscan_dbcreator_registry)
    {
        hdvb_chanscan_dbcreator_registry =
                g_hash_table_new(g_str_hash, g_str_equal);
    }
    if (!hdvb_chanstodb_registry)
    {
        hdvb_chanstodb_registry =
                g_hash_table_new(g_str_hash, g_str_equal);
    }
}

void
hdvb_chanscan_factory_register(const char *name,
        HdvbChannelScanFactory scanner_factory,
        HdvbDbCreator db_creator,
        HdvbScanToDbFactory chans_to_db_factory)
{
    hdvb_chanscan_factory_init_registry();
    g_hash_table_insert(hdvb_chanscan_factory_registry,
            (char *) name, scanner_factory);
    g_hash_table_insert(hdvb_chanscan_dbcreator_registry,
            (char *) name, db_creator);
    g_hash_table_insert(hdvb_chanstodb_registry,
            (char *) name, chans_to_db_factory);
}

static void
check_init(void)
{
    static gboolean inited = FALSE;
    
    if (!inited)
    {
        inited = TRUE;
        hdvb_chanscan_factory_register("default",
                hdvb_channel_scan_new, NULL, NULL);
        hdvb_chanscan_factory_register("Freesat",
                hdvb_freesat_scan_new, hdvb_freesat_db_creator,
                hdvb_freesat_scan_to_db_new);
        hdvb_chanscan_factory_register("Freeview",
                hdvb_freeview_scan_new, NULL, NULL);
    }
}

HdvbChannelScanFactory
hdvb_chanscan_factory_lookup_scanner(const char *name)
{
    HdvbChannelScanFactory fac;
    
    check_init();
    if (!name)
        name = "default";
    fac = g_hash_table_lookup(hdvb_chanscan_factory_registry, name);
    if (!fac)
    {
        g_warning(_("No channel scanner factory for '%s', using default"),
                name);
        fac = hdvb_channel_scan_new;
    }
    return fac;
}

HdvbDbCreator
hdvb_chanscan_factory_lookup_db_creator(const char *name)
{
    HdvbDbCreator creator;
    
    check_init();
    if (!name)
        name = "default";
    creator = g_hash_table_lookup(hdvb_chanscan_dbcreator_registry, name);
    if (!creator)
        return hdvb_chans_db_create;
    return creator;
}

HdvbScanToDbFactory
hdvb_chanscan_factory_lookup_chans_to_db(const char *name)
{
    HdvbScanToDbFactory fac;
    
    check_init();
    if (!name)
        name = "default";
    fac = g_hash_table_lookup(hdvb_chanstodb_registry, name);
    if (!fac)
    {
        fac = hdvb_chan_scan_to_db_new;
    }
    return fac;
}

